﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var canvas, context;
    var gameStage;
    var preload;
    var logoScreenImage, logoScreenBitmap;
    var newGame = true;

    var player;
    var floorImage, floorBitmap;
    var playerIdleImage, playerIdleBitmap;

    var scaleW = window.innerWidth / 1366;
    var scaleH = window.innerHeight / 768;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    function initialize() {

        canvas = document.getElementById("gameCanvas");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        context = canvas.getContext("2d");
        canvas.addEventListener("MSPointerUp", pointerUp, false);

        gameStage = new createjs.Stage(canvas);

        loadContent();

    }

    function loadContent() {          

        preload = new createjs.PreloadJS();

        preload.onComplete = prepareStage;

        var manifest = [
                { id: "logoScreen", src: "images/GFX/LogoScreen.png" },
                { id: "floor", src: "images/GFX/floor.png" },
                { id: "ghost", src: "images/GFX/Ghost.png" },
                { id: "playerIdle", src: "images/GFX/PlayerIdle.png" }
        ];

        preload.loadManifest(manifest);

    }

    function prepareStage() {

        logoScreenImage = preload.getResult("logoScreen").result;
        logoScreenBitmap = new createjs.Bitmap(logoScreenImage);
        gameStage.addChild(logoScreenBitmap);
        createjs.Ticker.setInterval(window.requestAnimationFrame);
        createjs.Ticker.addListener(gameLoop);

        logoScreenBitmap.scaleX = scaleW;
        logoScreenBitmap.scaleY = scaleH;
        logoScreenImage = preload.getResult("logoScreen").result;
        logoScreenBitmap = new createjs.Bitmap(logoScreenImage);
        logoScreenBitmap.scaleX = scaleW;
        logoScreenBitmap.scaleY = scaleH;
        gameStage.addChild(logoScreenBitmap);

        floorImage = preload.getResult("floor").result;
        floorBitmap = new createjs.Bitmap(floorImage);
        floorBitmap.visible = false;
        floorBitmap.scaleX = scaleW;
        floorBitmap.scaleY = scaleH;
        gameStage.addChild(floorBitmap);

        playerIdleImage = preload.getResult("playerIdle").result;
        playerIdleBitmap = new createjs.Bitmap(playerIdleImage);
        playerIdleBitmap.visible = false;
        playerIdleBitmap.scaleX = scaleW;
        playerIdleBitmap.scaleY = scaleH;
        gameStage.addChild(playerIdleBitmap);


    }

    function gameLoop() {
        update();
        draw();
    }

    function update() {
        if (newGame) {
            logoScreenBitmap.visible = true;
            playerIdleBitmap.visible = false;
            floorBitmap.visible = false;
        }
        else {
            logoScreenBitmap.visible = false;
            playerIdleBitmap.visible = true;
            floorBitmap.visible = true;
            if (player.targetX > player.positionX) {
                player.positionX += 3;
            }
            if (player.targetX < player.positionX) {
                player.positionX -= 3;
            }
            if (player.targetY > player.positionY) {
                player.positionY += 3;
            }
            if (player.targetY < player.positionY) {
                player.positionY -= 3;
            }
        }
    }

    function draw() {
        gameStage.update();
    }

    function pointerUp(event) {
        if (newGame) {
            newGame = false;
        }
        else {
        }
    }

    // The player class
    function Player() {
        this.positionX = window.innerWidth / 2;
        this.positionY = window.innerHeight / 2;

        this.targetX = this.positionX;
        this.targetY = this.positionY;

        this.width = playerIdleBitmap.image.width * scaleW;
        this.height = playerIdleBitmap.image.height * scaleH;
    }

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    document.addEventListener("DOMContentLoaded", initialize, false);

    app.start();
})();
